<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const input = [
            {
                username: 'zhangsan',
                starCount: 10,
                content: '感觉不会再爱了',
                timestamp: 1642579353575,
                replies: [
                    {
                        username: 'lisi',
                        starCount: 3,
                        content: '感觉不会再爱了',
                        timestamp: 1642579354575,
                        replies: [],
                    },
                    {
                        username: 'wangwu',
                        starCount: 3,
                        content: '感觉不会再爱了',
                        timestamp: 1642579355575,
                        replies: [
                            {
                                username: 'liuliu',
                                starCount: 1,
                                content: '感觉不会再爱了',
                                timestamp: 1642579357575,
                                replies: [],
                            },
                        ],
                    },
                ],
            },
            {
                username: 'yuqi',
                starCount: 99,
                content: '感觉不会再爱了',
                timestamp: 1642579323575,
                replies: [
                    {
                        username: 'zhaoqi',
                        starCount: 12,
                        content: '感觉不会再爱了',
                        timestamp: 1642579333575,
                        replies: [],
                    },
                ],
            },
        ];
        const output = [
            {
                username: 'zhangsan',
                // 总点赞数
                totalStarCount: 17,
                // 评论数
                commentCount: 3,
                content: '感觉不会再爱了',
                timestamp: 1642579353575,
            },
            {
                username: 'yuqi',
                totalStarCount: 111,
                commentCount: 1,
                content: '感觉不会再爱了',
                timestamp: 1642579353575,
            },
        ];
        function getCmtCount (replies) {
            let sum = 0;
            (function getCount (replies) {
                sum += replies.length;
                for (const item of replies) {
                    getCount(item.replies);
                }
            })(replies);
            return sum;
        }
        function getTotalStarCount (item) {
            let sum = 0;
            (function getCount (item) {
                sum += item.starCount;
                for (const subItem of item.replies) {
                    getCount(subItem);
                }
            })(item);
            return sum;
        }
        function totalStar(input) {
            return input.map(item => {
                return {
                    username: item.username,
                    totalStarCount: getTotalStarCount(item),
                    commentCount: getCmtCount(item.replies),
                    content: item.content,
                    timestamp: item.timestamp,
                }
            });
        }
        console.log(totalStar(input));
    </script>
</body>
</html>